package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtil;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.dao.payconfig.BisBankExtConfigDAO;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.UmPayConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.umpay.ebank.UmPayMessage;
import com.gopay.common.domain.bis.BaseBisBankExtConfig;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 联动优势网银直连对象封装
 * 
 * @UmPayManager.java
 * @author zhengxue
 * @2013-12-4 上午9:54:32 www.gopay.com.cn Inc.All rights reserved.
 */
public class UmPayManager implements BankManagerRemoteService {
	
	@Resource(name = "umPayConfig")
	private Map<String, String> umPayConfig;
	
	@Resource(name = "umPayBankServiceClientHna")
	private BankService umPayBankServiceClientHna;
	
	@Resource(name = "umPayBankServiceClient")
	private BankService umPayBankServiceClient;

	@Resource(name="bisBankExtConfigDAO")
	private BisBankExtConfigDAO bisBankExtConfigDAO;
	
	/**
	 * 签名
	 */
	public Object sign(BaseMessage msg) throws GopayException {
		// 获取银行接口通道配置信息
		BaseBisBankExtConfig bankConfig = bisBankExtConfigDAO.findExtConfigByBankChannelAndBankBusProd(msg.getBankBusProd(),msg.getBankIntrChann(), msg.getBank());
		msg.setBankOrderId("UM" + TransUtil.processOrderIdTo14Digit(msg.getOrderId()));
		//设置联动优势接口银行码
		msg.setSpecial2(bankConfig.getIntrfaceBankCode());
		UmPayMessage message = (UmPayMessage) msg;
		// 协议参数
		message.setService(UmPayConstants.UMPAY_EBANK_PAY);
		message.setCharset(umPayConfig.get("UMPAY.CHARSET"));
		message.setMerId(message.getMerID());
		message.setSignType(umPayConfig.get("UMPAY.SIGNTYPE"));
		message.setNotifyUrl(message.getPayRuturnUrl());
		message.setRetUrl(message.getPayNotifyUrl());
		message.setVersion(umPayConfig.get("UMPAY.VERSION"));
		// 业务参数
		message.setMerDate(message.getOrderDate().substring(0, 8));
		// 如果是人民币，则以分为单位
		message.setAmt(TransUtils.amountConvert2Fen(message.getAmount().toString()));
		message.setAmtType((String) umPayConfig.get("UMPAY.AMTTYPE"));
		message.setPayType((String) umPayConfig.get("UMPAY.PAYTYPE"));
		message.setGateId(message.getSpecial2());
		message.setMerPriv((String) umPayConfig.get("UMPAY.MERPRIV"));
		// 用户在创建交易时，该用户当前所使用机器的 IP。 用作防钓鱼校验
		message.setUserIp(message.getSpecial());
		message.setInterfaceType((String) umPayConfig.get("UMPAY.INTERFACETYPE"));
		BankLogUtils.printB2CManagerSignSendLog(message);
		String signMsg;
		if(message.isHna()){
			signMsg = (String) umPayBankServiceClientHna.sign(message);
		}else{
			signMsg = (String) umPayBankServiceClient.sign(message);
		}
		message.setUrl(signMsg);
		BankLogUtils.printB2CManagerSignReturnLog(message);
		return message;
	}

	/**
	 * 验签
	 */
	public Object verify(BaseMessage msg) throws GopayException {
		UmPayMessage message = (UmPayMessage) msg;
		BankLogUtils.printB2CManagerVerifySendLog(message);
		UmPayMessage resMsg;
		if(message.isHna()){
			resMsg = (UmPayMessage) umPayBankServiceClientHna.verify(message);
		}else{
			resMsg = (UmPayMessage) umPayBankServiceClient.verify(message);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(message, true);
		return resMsg;
	}

	@Override
	public Object query(Object obj) throws GopayException {
		UmPayMessage message = (UmPayMessage) obj;
		message.setService(UmPayConstants.UMPAY_EBANK_QUERY);
		message.setSignType(umPayConfig.get("UMPAY.SIGNTYPE"));
		message.setCharset(umPayConfig.get("UMPAY.CHARSET"));
		message.setMerId(message.getMerID());
		message.setVersion(umPayConfig.get("UMPAY.VERSION"));
		message.setMerDate(message.getOrderDate().substring(0, 8));
		BankLogUtils.printB2CManagerQuerySendLog(message, "");
		if(message.isHna()){
			message = (UmPayMessage) umPayBankServiceClientHna.query(message);
		}else{
			message = (UmPayMessage) umPayBankServiceClient.query(message);
		}
		BankLogUtils.printB2CManagerQueryReturnLog(message);
		return message;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
